Skip to content

fix: prevent deleting a file that is open without FileShare.Delete on Windows - #1615

Open
Asimmirzac wants to merge 1 commit into
TestableIO:mainfrom
Asimmirzac:fix/delete-open-file-windows
Open

Asimmirzac wants to merge 1 commit into
TestableIO:mainfrom
Asimmirzac:fix/delete-open-file-windows

Conversation

@Asimmirzac

Copy link
Copy Markdown

Fixes #894
Fixes #756

On Windows, File.Delete, FileInfo.Delete and a recursive Directory.Delete now throw an IOException when the file has an open stream that was opened without FileShare.Delete, like the real file system. On Linux and macOS the file can still be deleted while it is open, which matches those platforms.

To make FileShare.Delete work, File.Open(path, mode, access, share) and File.Open(path, FileStreamOptions) now pass the requested share mode to the stream; before, it was always FileShare.Read. As a result, the File.Open(path, mode) and File.Open(path, mode, access) overloads now apply the FileShare.None they already passed, as in .NET. Opening a file with FileMode.Create/Truncate while another stream has it open is still decided by the share check of the new stream.

Tests cover the Windows and Unix behaviour, FileInfo.Delete, a recursive directory delete, FileShare.Delete, and the share mode passed to File.Open. The Windows-only tests were not run locally (Linux only).

This change was prepared with AI assistance (Claude Code).

🤖 Generated with Claude Code

… Windows

On Windows, `File.Delete`, `FileInfo.Delete` and a recursive `Directory.Delete`
now throw an `IOException` when the file has an open stream that was opened
without `FileShare.Delete`, as the real file system does. Unix keeps allowing
the deletion.

`File.Open(path, mode, access, share)` and `File.Open(path, FileStreamOptions)`
now pass the requested share mode to the stream instead of always using
`FileShare.Read`.

Fixes TestableIO#894
Fixes TestableIO#756

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mergify

mergify Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IFileInfo.OpenRead() does not lock file when using MockFileSystem Deleting an open file is handled inconsistently

1 participant